package com.jd.sunny.exception;

/**
 * @author :Sun Lin
 * @ClassName:ExceptionEnum
 * @Description: 异常枚举类
 * @date :2012-9-10
 */
public enum ExceptionEnum {

    //ConfigUtilException
    ConfigUtilException_LOAD(2012000001, "ConfigUtilException_LOAD", "Load resources exception"),
    ConfigUtilException_GET(2012000002, "ConfigUtilException_GET", "Get value exception"),
    ConfigUtilException_SAVE_FileNotFoundException(2012000003, "ConfigUtilException_SAVE_FileNotFoundException", "File not found exception"),
    ConfigUtilException_SAVE_IOException(2012000004, "ConfigUtilException_SAVE_IOException", "Save IOException"),

    //DateUtilException
    DateUtilException_PARSE(2012000005, "DateUtilException_PARSE", "Parse Exception");


    private Integer code;
    private String name;
    private String description;

    /**
     * @param description 中文描述
     */
    private ExceptionEnum(String description) {
        this.description = description;
    }

    /**
     * @param code        数字编码
     * @param description 中文描述
     */
    private ExceptionEnum(Integer code, String description) {
        this.code = code;
        this.description = description;
    }

    /**
     * @param name        英文编码名称
     * @param description 中文描述
     */
    private ExceptionEnum(String name, String description) {
        this.name = name;
        this.description = description;
    }

    /**
     * @param code        数字编码
     * @param name        英文编码名称
     * @param description 中文描述
     */
    private ExceptionEnum(Integer code, String name, String description) {
        this.code = code;
        this.name = name;
        this.description = description;
    }


    /**
     * 获取枚举类型数值编码
     */
    public Integer toCode() {
        return this.code == null ? this.ordinal() : this.code;
    }

    /**
     * 获取枚举类型英文编码名称
     */
    public String toName() {
        return this.name == null ? this.name() : this.name;
    }

    /**
     * 获取枚举类型中文描述
     */
    public String toDescription() {
        return this.description;
    }

    /**
     * 获取枚举类型中文描述
     */
    public String toString() {
        return this.description;
    }

    /**
     * 按数值获取对应的枚举类型
     *
     * @param code 数值
     * @return 枚举类型
     */
    public static ExceptionEnum enumValueOf(Integer code) {
        ExceptionEnum[] values = ExceptionEnum.values();
        ExceptionEnum v = null;
        for (int i = 0; i < values.length; i++) {
            if (code.equals(values[i].toCode())) {
                v = values[i];
                break;
            }
        }
        return v;
    }

    /**
     * 按英文编码获取对应的枚举类型
     *
     * @param name 英文编码
     * @return 枚举类型
     */
    public static ExceptionEnum enumValueOf(String name) {
        ExceptionEnum[] values = ExceptionEnum.values();
        ExceptionEnum v = null;
        for (int i = 0; i < values.length; i++) {
            if (name.equalsIgnoreCase(values[i].toName())) {
                v = values[i];
                break;
            }
        }
        return v;
    }

}
